<template>
    <div>
        <h-section title="研发流程信息" collapse>
            <a-form layout="inline">
                <a-row>
                    <a-col :span="16">
                        <a-form-item label="当前阶段" :span="24">
                            <div class="font-w-b">{{ stage }}</div>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
            <common-test-file ref="commonTestFileRef" :detail="detail" :stage="stage" :disabled="disabled" @detailChange="detailChange" />
            <slot name="processHistoryRecord" />
        </h-section>
    </div>
</template>

<script lang="ts">
import CommonTestFile from '@/views/version/versionDetail/components/RelateProcess/CheckProcess/components/CommonTestFile.vue';
import { defineComponent, ref, unref, computed, h } from 'vue';
export default defineComponent({
    name: 'RequireDev',
    components: {
        CommonTestFile
    },
    props: {
        detail: { type: Object, default: () => ({}) },
        stage: { type: String, default: '' },
        disabled: { type: Boolean, default: false }
    },
    emits: ['save', 'detailChange'],
    setup(props, { emit }) {
        const commonTestFileRef = ref<any>();
        const isAutoTest = computed(() => {
            return unref(commonTestFileRef)?.isAutoTest;
        });
        // 验证
        const handleValidate = () => {
            return new Promise((res) => {
                unref(unref(commonTestFileRef).ruleFormRef)
                    .validate()
                    .then(
                        () => {
                            res(true);
                        },
                        () => {
                            res(false);
                        }
                    );
            });
        };
        const handleSave = async (type: number) => {
            let res: any = true;
            const { id, instanceId } = props.detail;
            const query = {
                id,
                instanceId,
                ...unref(commonTestFileRef).form
            };
            if (unref(commonTestFileRef).ruleFormRef) {
                res = await handleValidate();
            }
            if (res) {
                emit('save', {
                    type,
                    query
                });
            }
        };
        const detailChange = () => {
            handleSave(9);
        };
        return {
            commonTestFileRef,
            isAutoTest,
            handleSave,
            detailChange
        };
    }
});
</script>

<style scoped lang="less">
@import './styles/index.less';
</style>